package edu.mobbuzz.storage;

import edu.mobbuzz.bean.RSSCategory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;


public class RSSCategoryRecordStore {
    private final int MAX_RSS_CATEGORY = 500;
    private RecordStore rsRSSCategory;
    public RSSCategory[] rssCategoryArr;
    private int nbRSSCategory;
    
    private int tempInt;
    private byte []data;    
    
    ByteArrayOutputStream bout;
    DataOutputStream dout;
    
    
    public RSSCategoryRecordStore(){
        /*try {
            RecordStore.deleteRecordStore("Contacts");
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        
        }*/
        openRecStore();
    }
    public boolean openRecStore () {
        try {
            rsRSSCategory = RecordStore.openRecordStore("RSS_CATEGORY", true);
            rssCategoryArr = new RSSCategory[MAX_RSS_CATEGORY];
            setNbRSSCategory(rsRSSCategory.getNumRecords());
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } 
        return true;
    }
 
        
    public boolean readrsRSSCategory () {
        try {
            RecordEnumeration reRSSCategory = rsRSSCategory.enumerateRecords (null,null,false);

            reRSSCategory.reset();
            tempInt = 0;		
            while (reRSSCategory.hasNextElement()) {
                rssCategoryArr[tempInt] = new RSSCategory();
                readRSSCategory(reRSSCategory.nextRecordId(), rssCategoryArr[tempInt++]);
            }
            reRSSCategory.destroy();
        } catch (RecordStoreException e) {
                e.printStackTrace();
                return false;
        }
        return true;
    }
    
    public void addRSSCategory(RSSCategory rssCategory){
        if (getNbRSSCategory() < MAX_RSS_CATEGORY) {
            try {
                bout = new ByteArrayOutputStream();
                dout = new DataOutputStream(bout); 
                dout.writeInt(rssCategory.getRecIdRSS());
                dout.writeInt(rssCategory.getRecIdCategory());
                dout.flush();
                data = bout.toByteArray();
                bout.reset();
                rssCategoryArr[nbRSSCategory] = new RSSCategory();
                rssCategoryArr[getNbRSSCategory()].setRecId(rsRSSCategory.addRecord( data, 0, data.length ));
                rssCategoryArr[getNbRSSCategory()].setRecIdRSS(rssCategory.getRecIdRSS());
                rssCategoryArr[getNbRSSCategory()].setRecIdCategory(rssCategory.getRecIdCategory());
                
            } catch( Exception e ) {
                if (e instanceof RecordStoreFullException){
                        
                }
                e.printStackTrace ();
            }
        } else {

        }
    }

    public void updateRSSCategory (int index, RSSCategory rssCategory) {
        try {
            dout.writeInt(rssCategory.getRecIdRSS());
            dout.writeInt(rssCategory.getRecIdCategory());
            dout.flush();
            data = bout.toByteArray();
            rsRSSCategory.setRecord(rssCategory.getRecId(), data, 0, data.length );
            bout.reset();
            rssCategoryArr[index].setRecIdRSS(rssCategory.getRecIdRSS());
            rssCategoryArr[index].setRecIdCategory(rssCategory.getRecIdCategory());
        } catch( Exception e ) {
            e.printStackTrace();
        }
    }

    public void deleteRSSCategory (int index) {
        try {
            rsRSSCategory.deleteRecord(rssCategoryArr[index].getRecId());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        setNbRSSCategory(getNbRSSCategory() - 1);
        for (tempInt=index; tempInt<getNbRSSCategory(); tempInt++) {
            rssCategoryArr[tempInt] = rssCategoryArr[tempInt+1];
        }
        
    }
        
    public boolean readRSSCategory( int id, RSSCategory rssCategory    ){
        data = new byte[110];
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream din = new DataInputStream(bin);

        if( rsRSSCategory != null ){
            try {
                rsRSSCategory.getRecord( id, data, 0 );
                rssCategory.setRecId(id);
                rssCategory.setRecIdRSS(din.readInt());
                rssCategory.setRecIdCategory(din.readInt());
                din.reset();
            } catch( Exception e ) {
                return false;
            }
        }
        return true;
        
    }  
    

    /**
     * @return the nbRSSCategory
     */
    public int getNbRSSCategory() {
        return nbRSSCategory;
    }

    /**
     * @param nbRSSCategory the nbRSSCategory to set
     */
    public void setNbRSSCategory(int nbRSSCategory) {
        this.nbRSSCategory = nbRSSCategory;
    }
    
    
}